

<script type="text/javascript" src="<?php echo base_url(); ?>js/autoNumeric-1.7.5.js"></script>
<script type="text/javascript">
	function removeRow(id){
		$('#row-'+id).hide();
	
	}
	
	function autoComplete(id){
		var ac_config = {
			source: "<?php echo base_url(); ?>index.php/produksi_rph/auto_complete_rph",
			select: function(event, ui){
				$('#rph-'+id).val(ui.item.NAMA);
				$('#id-rph-'+id).val(ui.item.ID_TITIK_PASOK);
				
				
				$.ajax({
					url: "<?= base_url(); ?>index.php/produksi_rph/show_rph_list",
					type: "POST",
					data: "id_rph="+ui.item.ID_TITIK_PASOK+"&id="+id,
					success: function(msg){
						
						$('#rph-content-'+id).append(msg);

					}
				});
				
				
				
			},
			minLength:1
		};
		$('#rph-'+id).autocomplete(ac_config);
	}
		
	$(document).ready(function(){
		function addRow(id){
			var row = '<div class="row-fluid sortable" id="row-'+id+'"><div class="box span12">';
			row += '<div class="box-header well" data-original-title><h2>RPH '+id+'</h2>';
			if(id > 1){
				row += '<div class="box-icon"><a onclick="removeRow('+id+');" href="#" id="remove-button-'+id+'" class="btn btn-round"><i class="icon-remove"></i></a></div>';
			}
			row += '</div>';
			
			row += '<div class="box-content">';
			
			row += 'Nama Rumah Potong Hewan&nbsp&nbsp';
			row += '<input type="text" id="rph-'+id+'" onkeyup="autoComplete('+id+');" name="rph-'+id+'"><input type="hidden" id="id-rph-'+id+'">';
			row += '<div id="rph-content-'+id+'"></div>';
			
			row += '</div></div></div>';
			
			$('#rph-content').append(row);
		}
		
		
		
		$('#tambah-button').click(function(){
			var num = $('#jumlah_data').val();
			var total = rowNum;
			for(var i=rowNum; i<(parseInt(num)+parseInt(rowNum)); i++){
				addRow(i);
				total++;
			}
			
			rowNum = total;
		});
		
		var rowNum = 2;
		
		$('#jumlah_data').keyup(function()
		{
			$('#jumlah_data').autoNumeric({
				aSep:'.',
				dGroup:'3',
				aDec:',',
				vMax:'999999999999',	
				vMin:'0'
			});
			
		});
		
		$('#simpan-button').click(function(){
			var jumlahSapi = parseInt(<?= $jenis_sapi_count; ?>);//alert(jumlahSapi);
			for(var i=1; i<rowNum; i++){
				var idTitikPasok = $('#id-rph-'+i).val();
				
				for(var j=1; j<=jumlahSapi; j++){
					/* Jantan start */
					var jantanID = $('#jantan-id-jenis-sapi-'+i+'-'+j).val();
					
					/* less than 6 yrs */
					
					var jantanKomentarlt6 = $('#jantan-komentar-lt6-'+i+'-'+j).val();
					
					var jantanPotlt6 = $('#jantan-dipotong-lt6-'+i+'-'+j).val();
					var jantanProdlt6 = $('#jantan-terproduksi-lt6-'+i+'-'+j).val();
					var jantanDaginglt6 = $('#jantan-daging-lt6-'+i+'-'+j).val();
					
					if(jantanDaginglt6 != '' && jantanPotlt6 != '' && jantanPotlt6 != null && jantanProdlt6 != null && jantanProdlt6 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanPotlt6, jantanProdlt6, 0, 0, 1, jantanKomentarlt6, null, jantanDaginglt6);
					}
					
					var jantanUmurlt6 = $('#jantan-umur-lt6-'+i+'-'+j).val();
					if(jantanUmurlt6 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanUmurlt6, 0, 3, 0, 1, jantanKomentarlt6, 0, jantanDaginglt6);
					}
					
					var jantanBuntinglt6 = $('#jantan-bunting-lt6-'+i+'-'+j).val();
					if(jantanBuntinglt6 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanBuntinglt6, 0, 3, 0, 1, jantanKomentarlt6, 1, jantanDaginglt6);
					}
					
					var jantanLainnyalt6 = $('#jantan-lainnya-lt6-'+i+'-'+j).val();
					if(jantanLainnyalt6 != ''){//alert();
						insertToProdRPH(idTitikPasok, jantanID, jantanLainnyalt6, 0, 3, 0, 1, jantanKomentarlt6, 2, jantanDaginglt6);
					}
					
					/* between 6 - 8 yrs */
					
					var jantanKomentarbt68 = $('#jantan-komentar-bt6-8-'+i+'-'+j).val();
					
					var jantanPotbt68 = $('#jantan-dipotong-bt6-8-'+i+'-'+j).val();
					var jantanProdbt68 = $('#jantan-terproduksi-bt6-8-'+i+'-'+j).val();
					var jantanDagingbt68 = $('#jantan-daging-bt6-8-'+i+'-'+j).val();
					
					if(jantanDagingbt68 != '' && jantanPotbt68 != '' && jantanProdbt68 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanPotbt68, jantanProdbt68, 0, 0, 1, jantanKomentarbt68, null, jantanDagingbt68);
					}
					
					var jantanUmurbt68 = $('#jantan-umur-bt6-8-'+i+'-'+j).val();
					if(jantanUmurbt68 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanUmurbt68, 0, 3, 0, 1, jantanKomentarbt68, 0, jantanDagingbt68);
					}
					
					var jantanBuntingbt68 = $('#jantan-bunting-bt6-8-'+i+'-'+j).val();
					if(jantanBuntingbt68 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanBuntingbt68, 0, 3, 0, 1, jantanKomentarbt68, 1, jantanDagingbt68);
					}
					
					var jantanLainnyabt68 = $('#jantan-lainnya-bt6-8-'+i+'-'+j).val();
					if(jantanLainnyabt68 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanLainnyabt68, 0, 3, 0, 1, jantanKomentarbt68, 2, jantanDagingbt68);
					}
					
					/* greater than 8 yrs */
					
					var jantanKomentargt8 = $('#jantan-komentar-gt8-'+i+'-'+j).val();
					
					var jantanPotgt8 = $('#jantan-dipotong-gt8-'+i+'-'+j).val();
					var jantanProdgt8 = $('#jantan-terproduksi-gt8-'+i+'-'+j).val();
					var jantanDaginggt8 = $('#jantan-daging-gt8-'+i+'-'+j).val();
					
					if(jantanDaginggt8 != '' && jantanPotgt8 != '' && jantanProdgt8 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanPotgt8, jantanProdgt8, 0, 0, 1, jantanKomentargt8, null, jantanDaginggt8);
					}
					
					var jantanUmurgt8 = $('#jantan-umur-gt8-'+i+'-'+j).val();
					if(jantanUmurgt8 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanUmurgt8, 0, 3, 0, 1, jantanKomentargt8, 0, jantanDaginggt8);
					}
					
					var jantanBuntinggt8 = $('#jantan-bunting-gt8-'+i+'-'+j).val();
					if(jantanBuntinggt8 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanBuntinggt8, 0, 3, 0, 1, jantanKomentargt8, 1, jantanDaginggt8);
					}
					
					var jantanLainnyagt8 = $('#jantan-lainnya-gt8-'+i+'-'+j).val();
					if(jantanLainnyagt8 != ''){
						insertToProdRPH(idTitikPasok, jantanID, jantanLainnyagt8, 0, 3, 0, 1, jantanKomentargt8, 2, jantanDaginggt8);
					}
					
					/* jantan end */
					/* betina start */
					
					var betinaID = $('#betina-id-jenis-sapi-'+i+'-'+j).val();
					
					/* less than 6 yrs */
					
					var betinaKomentarlt6 = $('#betina-komentar-lt6-'+i+'-'+j).val();
					
					var betinaPotlt6 = $('#betina-dipotong-lt6-'+i+'-'+j).val();
					var betinaProdlt6 = $('#betina-terproduksi-lt6-'+i+'-'+j).val();
					var betinaDaginglt6 = $('#betina-daging-lt6-'+i+'-'+j).val();
					
					if(betinaDaginglt6 != '' && betinaPotlt6 != '' && betinaProdlt6 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaPotlt6, betinaProdlt6, 0, 0, 0, betinaKomentarlt6, null, betinaDaginglt6);
					}
					
					var betinaUmurlt6 = $('#betina-umur-lt6-'+i+'-'+j).val();
					if(betinaUmurlt6 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaUmurlt6, 0, 3, 0, 0, betinaKomentarlt6, 0, betinaDaginglt6);
					}
					
					var betinaBuntinglt6 = $('#betina-bunting-lt6-'+i+'-'+j).val();
					if(betinaBuntinglt6 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaBuntinglt6, 0, 3, 0, 0, betinaKomentarlt6, 1, betinaDaginglt6);
					}
					
					var betinaLainnyalt6 = $('#betina-lainnya-lt6-'+i+'-'+j).val();
					if(betinaLainnyalt6 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaLainnyalt6, 0, 3, 0, 0, betinaKomentarlt6, 2, betinaDaginglt6);
					}
					
					/* between 6 - 8 yrs */
					
					var betinaKomentarbt68 = $('#betina-komentar-bt6-8-'+i+'-'+j).val();
					
					var betinaPotbt68 = $('#betina-dipotong-bt6-8-'+i+'-'+j).val();
					var betinaProdbt68 = $('#betina-terproduksi-bt6-8-'+i+'-'+j).val();
					var betinaDagingbt68 = $('#betina-daging-bt6-8'+i+'-'+j).val();
					
					if(betinaDagingbt68 != '' && betinaPotbt68 != '' && betinaProdbt68 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaPotbt68, betinaProdbt68, 2, 0, 0, betinaKomentarbt68, null, betinaDagingbt68);
					}
					
					var betinaUmurbt68 = $('#betina-umur-bt6-8-'+i+'-'+j).val();
					if(betinaUmurbt68 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaUmurbt68, 0, 3, 0, 0, betinaKomentarbt68, 0, betinaDagingbt68);
					}
					
					var betinaBuntingbt68 = $('#betina-bunting-bt6-8-'+i+'-'+j).val();
					if(betinaBuntingbt68 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaBuntingbt68, 0, 3, 0, 0, betinaKomentarbt68, 1, betinaDagingbt68);
					}
					
					var betinaLainnyabt68 = $('#betina-lainnya-bt6-8-'+i+'-'+j).val();
					if(betinaLainnyabt68 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaLainnyabt68, 0, 3, 0, 0, betinaKomentarbt68, 2, betinaDagingbt68);
					}
					
					/* greater than 8 yrs */
					
					var betinaKomentargt8 = $('#betina-komentar-gt8-'+i+'-'+j).val();
					
					var betinaPotgt8 = $('#betina-dipotong-gt8-'+i+'-'+j).val();
					var betinaProdgt8 = $('#betina-terproduksi-gt8-'+i+'-'+j).val();
					var betinaDaginggt8 = $('$betina-daging-gt8-'+i+'-'+j).val();
					
					if(betinaDaginggt8 != '' && betinaPotgt8 != '' && betinaProdgt8 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaPotgt8, betinaProdgt8, 2, 0, 0, betinaKomentargt8, null, betinaDaginggt8);
					}
					
					var betinaUmurgt8 = $('#betina-umur-gt8-'+i+'-'+j).val();
					if(betinaUmurgt8 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaUmurgt8, 0, 3, 0, 0, betinaKomentargt8, 0, betinaDaginggt8);
					}
					
					var betinaBuntinggt8 = $('#betina-bunting-gt8-'+i+'-'+j).val();
					if(betinaBuntinggt8 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaBuntinggt8, 0, 3, 0, 0, betinaKomentargt8, 1, betinaDaginggt8);
					}
					
					var betinaLainnyagt8 = $('#betina-lainnya-gt8-'+i+'-'+j).val();
					if(betinaLainnyagt8 != ''){
						insertToProdRPH(idTitikPasok, betinaID, betinaLainnyagt8, 0, 3, 0, 0, betinaKomentargt8, 2, betinaDaginggt8);
					}
					
					/* Betina end */
				}
				
			}
			
			window.location = "<?= base_url(); ?>produksi_rph";
		});
		
		function insertToProdRPH(idTitikPasok, idsapi, sapi, produksi, status, umur, kelamin, komentar, ditolak, idDaging){
			$.ajax({
				url: "<?= base_url(); ?>produksi_rph/insert_to_prod_rph",
				type: "POST",
				data: "id_titik_pasok="+idTitikPasok+"&id_sapi="+idsapi+"&jumlah_sapi="+sapi+"&id_daging="+idDaging+"&produksi_daging="+produksi+"&jenis_kelamin="+kelamin+"&kategori_umur="+umur+"&status="+status+"&komentar="+komentar+"&alasan_ditolak="+ditolak,
				success: function(msg){
					if(msg != ''){
						$('#error-message').append(msg);
					} else {
						
					}
					//alert(msg);
					//window.location = "<?= base_url(); ?>produksi_rph";
				}
			});
		}
		
		addRow(1);
	});
</script>

<div id="error-message"></div>

<div class="row-fluid sortable">
	<div class="box span6">
		<div class="box-header well" data-original-title>
			<h2><i class="icon-info-sign"></i> Parameter</h2>
			
		</div>
		<div class="box-content form-inline">
			Jumlah Data:
			&nbsp&nbsp
			<input name="jumlah_data" id="jumlah_data" type="text" >
			<button class="btn" id="tambah-button">Tambah</button>
		</div>
	</div>
	
	<div id="rph-content">
		
	</div>
	<button class="btn" id="simpan-button">Simpan</button>
</div>


		
